package kevin.study.zephyr.domain.model;

import kevin.study.zephyr.domain.model.base.Base;
import kevin.study.zephyr.exceptions.EndlessLoopException;
import kevin.study.zephyr.platform.Platform;

import java.util.Collection;


/**
 * 表单,展示与生成Document文档的模板
 *
 * @author Kevin.Huang
 * @createtime 2007-9-20
 */
public class Form extends Base {

    /**
     * name对应的字段名
     */
    public static final String FILTER_NAME = "name";

    /**
     *
     */
    private static final long serialVersionUID = 8080358763980880872L;

    private String name;

    private Collection<Form> subForms;

    private Collection<Field> fields;

    /**
     * 保存表单设计
     * @return 被保存的Form
     * @throws EndlessLoopException 验证不通过,Form有循环应用
     */
    public Form save() throws EndlessLoopException {
        try {
            if (this.validateForm(subForms)) {
                return Platform.getFormManager().getFormDAO().saveAndUpdate(this);
            } else {
                throw new EndlessLoopException("Endless LOOP in setSubForms");
            }
        } catch (StackOverflowError e) {
            // XXX 记录异常
            throw new EndlessLoopException("Endless LOOP in setSubForms", e);
        }

    }

    /**
     * 删除表单
     */
    public void delete() {
        Platform.getFormManager().getFormDAO().delete(this);
    }

    /**
     * 验证subForm是否合法(不能包含自己,否则会出现循环层次)
     *
     * @return true 验证通过 false 验证不通过有死循环
     *
     * @throws StackOverflowError 出现死循环会抛出此异常
     */
    public boolean validate() throws StackOverflowError {
        return validateForm(this.subForms);
    }

    /**
     * 验证subForm是否合法(不能包含自己,否则会出现循环层次)..做递归实现
     *
     * @param forms 需验证的form
     *
     * @return true 验证通过 false 验证不通过有死循环
     *
     * @throws StackOverflowError 出现死循环会抛出此异常
     */
    private boolean validateForm(Collection<Form> forms) throws StackOverflowError {
        if (forms != null) {
            for (Form form : forms) {
                if (form.equals(this)) {
                    return false;
                }
                if (form.getSubForms() != null) {
                    return validateForm(form.getSubForms());
                }
            }
            return true;
        } else {
            return true;
        }
    }

    /**
     * @return the subForms
     */
    public Collection<Form> getSubForms() {
        return subForms;
    }

    /**
     * @param subForms the subForms to set
     *
     * @throws EndlessLoopException 验证不通过,Form有循环应用
     */
    public void setSubForms(Collection<Form> subForms) throws EndlessLoopException {
        try {
            if (this.validateForm(subForms)) {
                this.subForms = subForms;
            } else {
                throw new EndlessLoopException("Endless LOOP in setSubForms");
            }
        } catch (StackOverflowError e) {
            // XXX 记录异常
            throw new EndlessLoopException("Endless LOOP in setSubForms", e);
        }
    }

    /**
     * @return the fields
     */
    public Collection<Field> getFields() {
        return fields;
    }

    /**
     * @param fields the fields to set
     */
    public void setFields(Collection<Field> fields) {
        this.fields = fields;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
	public void setName(String name) {
		this.name = name;
	}

}
